#include <iostream>
#include <set>
#include <utility>

using std::cout;
using std::endl;
using std::set;
using std::pair;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem :  con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void test()
{
    //set的基本特征
    //1、存放的是key类型，key值是唯一的，不能重复
    //2、默认情况下，会按照key值进行升序排列
    //3、底层实现使用的是红黑树
    set<int> number = {1, 3, 7, 9, 8, 7, 6, 3, 1};
    /* set<int, std::greater<int>> number = {1, 3, 7, 9, 8, 7, 6, 3, 1}; */
    display(number);

    //set的查找
    cout << endl << "set的查找" << endl;
    /* set<int>::size_type cnt = number.count(3); */
    size_t cnt = number.count(3);
    cout << "cnt = " << cnt << endl;

    auto it = number.find(5);
    if(it != number.end())
    {
        cout << "元素是存在的 " << *it << endl;
    }
    else
    {
        cout << "该元素不存在 " << endl;
    }

    cout << endl << "set的insert操作" << endl;
    /* auto ret = number.insert(5); */
    pair<set<int>::iterator, bool> ret = number.insert(5);
    if(ret.second)
    {
        cout << "插入成功 " <<  *ret.first << endl;
    }
    else
    {
        cout << "插入失败,元素已经存在set中" 
             <<  *ret.first << endl;
    }
    display(number);

    cout << endl << "set的删除" << endl;
    it = number.end();
    --it;
    cout << "*it = " << *it << endl;
    number.erase(it);
    display(number);

    cout << endl << "set不支持下标" << endl;
    /* cout << "number[1] = " << number[1] << endl; */


    cout << endl << "使用迭代器修改元素" << endl;
    it = number.begin();
    cout << "*it = " << *it << endl;
    /* *it = 100;//error */
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

